<template>
  <van-icon
    :color="value == 1 ? 'red' : ''"
    :name="value == 1 ? 'good-job' : 'good-job-o'"
    @click="onCollect"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'LikeArticle',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {}
  },
  computed: {},
  watch: {},
  created () { },
  methods: {
    async onCollect () {
      let status = 0
      if (this.value === 1) {
        // 有=> 取消
        await deleteLike(this.articleId)
        status = -1
      } else {
        // 无=> 点赞
        await addLike(this.articleId)
        status = 1
      }
      this.$emit('input', status)
      // 提示
      this.$toast(status === 1 ? '点赞成功' : '取消点赞')
    }
  }
}
</script>

<style scoped>
</style>
